﻿using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Http
{
    public class RequestItem
    {
        public RequestItem()
        {
            #region Init Headers

            Headers.Add("Accept-Language", "zh-cn");

            #endregion Init Headers
        }

        public bool Allowautoredirect { get; set; } = false;
        public CookieCollection CookieCollection { get; set; } = new CookieCollection();
        public Encoding Encoding { get; set; } = null;
        /// <summary>
        /// Default value is GET
        /// </summary>
        public Method Method { get; set; } = Method.GET;

        public string MethodString
        {
            set
            {
                Method = Method.GET;
                if (value.ToUpper() == Method.POST.ToString())
                {
                    Method = Method.POST;
                }
            }
        }
        /// <summary>
        /// Default value is "*/*"
        /// </summary>
        public string Accept { get; set; } = "*/*";
        /// <summary>
        /// Defalue value is "application/x-www-form-urlencoded"
        /// </summary>
        public string ContentType { get; set; } = "application/x-www-form-urlencoded";
        public string PostData { get; set; } = string.Empty;
        public WebProxy Proxy { get; set; }

        public string ProxyString
        {
            set { Proxy = new WebProxy(value); }
        }

        public string Referer { get; set; } = string.Empty;
        public string Url { get; set; } = string.Empty;
        /// <summary>
        /// Defalut value is "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)"
        /// </summary>
        public string UserAgent { get; set; } = "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1)";
        public WebHeaderCollection Headers { get; set; } = new WebHeaderCollection();

        public string HeadersString
        {
            set
            {
                foreach (var line in Regex.Split(value.Trim(), "\r\n"))
                {
                    var arr = Regex.Split(line.Replace(" ", ""), ":");
                    if (arr.Length != 2) continue;
                    var key = arr[0].Trim();
                    var val = arr[1].Trim();
                    switch (key)
                    {
                        case "User-Agent":
                            UserAgent = val;
                            break;
                        case "Referer":
                            Referer = val;
                            break;
                        case "ContentType":
                            ContentType = val;
                            break;
                        case "Accept":
                            Accept = val;
                            break;
                    }
                }
            }
        }
        /// <summary>
        /// Defalut value is true
        /// </summary>
        public bool KeepAlive { get; set; } = true;
        /// <summary>
        /// Default value is 10000
        /// </summary>
        public int Timeout { get; set; } = 10000;
        /// <summary>
        /// Default value is false
        /// </summary>
        public bool Expect100Continue { get; set; } = false;
    }

    public enum Method
    {
        // ReSharper disable once InconsistentNaming
        GET,
        // ReSharper disable once InconsistentNaming
        POST
    }
}